<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="roleName">
        <el-input v-model="queryParams.title" placeholder="请输入关键字" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="信息状态" clearable style="width: 240px">
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="swiperList" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="标题" prop="title" align="center" width="200" />
      <el-table-column label="缩略图" width="80">
        <template slot-scope="scope">
          <!-- {{scope.row.imgList}} -->
          <table-image :imgUrls="scope.row.imgList"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basics_information_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="顺序" prop="orderNum" align="center" width="200" />
      <el-table-column label="链接" prop="url" class-name="small-padding fixed-width" />
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm">
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="状态选择" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.basics_information_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="缩略图" prop="imgList">
          <image-upload :limit="1" :valueData="form.imgList" params="imgList" @uploaded="uploaded"></image-upload>
        </el-form-item>
        <el-form-item label="链接" prop="url">
          <el-input v-model="form.url" placeholder="请输入链接" />
        </el-form-item>
        <el-form-item label="顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="1" class="input-W" />
        </el-form-item>
      </el-form>
    </Z-Dialog>
  </div>
</template>

<script>
import { listSwiper, editBaselink, addBaselink, removeBaselink } from "@/api/basics/swiper.js";
import tableImage from "@/components/TableImageView/index.vue";
import { deepClone } from '@/utils/index'

export default {
  name: "rotation",
  dicts: ["basics_information_status", "basics_information_type"],
  components: { tableImage },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      swiperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        imgList: undefined,
        title: undefined,
        status: undefined,
        orderNum: undefined,
        url: undefined,
      },

      // 表单参数
      form: {},

      rules: {
        imgList: [{ required: true, message: "必填", trigger: "blur" }],
        title: [{ required: true, message: "必填", trigger: "blur" }],
      }
    };
  },
  computed: {
    //状态列表添加全部
    statusList() {
      let statusDictArray = JSON.parse(
        JSON.stringify(this.dict.type.basics_information_status)
      );
      statusDictArray.unshift({ label: "全部", value: undefined });
      return statusDictArray;
    },
  },

  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 获取轮播图列表 */
    getList() {
      this.loading = true;
      listSwiper(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.swiperList = response.data;
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form = {};
      this.open = true;
      this.title = "新增轮播图";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "轮播图编辑";
      this.form = deepClone(row);
      this.form.status = String(row.status);
      this.reset();
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editBaselink(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBaselink(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    uploaded(params, dict) {
      console.log(dict);
      // this.form[params] = JSON.stringify(dict);

      this.form[params] = dict;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除这条数据？')
        .then(() => {
          removeBaselink({ id: row.id }).then((response) => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        }
        ).catch(() => { });
    },
  },


};
</script>
